package com.whfc.iot.fse.lift.processor;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.enums.LiftDeviceStatus;
import com.whfc.common.util.JSONUtil;
import com.whfc.common.util.MathUtil;
import com.whfc.fse.dto.FseLiftDataDTO;
import com.whfc.iot.fse.lift.entity.LiftMsg0x10;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-05-20 11:06
 */
@Component
public class LiftUpMsgProcessor0x10 implements LiftUpMsgProcessor<LiftMsg0x10> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void processMsg(LiftMsg0x10 msg) {
        String textMsg = JSONUtil.toString(msg);
        logger.info("处理升降机实时数据命令:{}", textMsg);
        try {
            FseLiftDataDTO fseLiftData = new FseLiftDataDTO();
            fseLiftData.setPlatform(DevicePlatform.whfciot.name());
            fseLiftData.setSn(msg.getDeviceSn() + "");
            fseLiftData.setTime(msg.getTime());
            fseLiftData.setFloor(msg.getFloor());
            fseLiftData.setWindSpeed(MathUtil.round(msg.getWindSpeed()*0.1,1));
            fseLiftData.setDipAngle(MathUtil.round(0.01*(msg.getDipAngle1() - 1500),2));
            fseLiftData.setWeight((double) msg.getWeight());
            fseLiftData.setHeight(MathUtil.round(0.1 * msg.getHeight(),1));
            fseLiftData.setPersonNo(msg.getPersonNo());
            fseLiftData.setSpeed(MathUtil.round(0.1*msg.getSpeed(),1));
            fseLiftData.setFrontDoorState(transDoorState(msg.getFrontDoor()));
            fseLiftData.setBackDoorState(transDoorState(msg.getBackDoor()));
            // 设置报警类型
            setLiftStatusType(msg,fseLiftData);
            amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSONUtil.toString(fseLiftData));
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }

    private void setLiftStatusType(LiftMsg0x10 msg ,FseLiftDataDTO fseLiftData) {
        fseLiftData.setLiftStatus(0);
        if (2 == msg.getWeightState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_WEIGH_OVERLOAD.getValue());
        }
        if (2 == msg.getHeight()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_HEIGHT_RUSH_TOP.getValue());
        }
        if (2 == msg.getSpeedState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_SPEED.getValue());
        }
        if (2 == msg.getPersonNoState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_PEOPLE_OVERSTEP.getValue());
        }
        if (2 == msg.getDipState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_DIP_ANGLE.getValue());
        }
        if (1 == msg.getFrontDoorState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_FRONT_GATE.getValue());
        }
        if (1 == msg.getBackDoorState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_BACK_GATE.getValue());
        }
        if (2 == msg.getWindSpeedState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_WINDSPEED.getValue());
        }
        if (1 == msg.getUpState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_UP.getValue());
        }
        if (1 == msg.getFallState()) {
            fseLiftData.setLiftStatus(LiftDeviceStatus.LIFT_FALL.getValue());
        }
    }

    /**
     * 元数据转换为数据库值
     *
     * @param doorState
     * @return
     */
    private int transDoorState(int doorState) {
        return doorState + 1;
    }
}
